#include "quit_widget.h"

Quit_Widget::Quit_Widget(bool _is_determine,QWidget *parent):QWidget(parent)
{
    m_move = false;
    is_determine = _is_determine;
    setFixedSize(400,240);
    setWindowFlags(Qt::WindowStaysOnTopHint|Qt::Tool);

    tip_label = new QLabel();

    massge_label = new QLabel();

    close_btn = new QPushButton();
    close_btn->setFixedSize(30,30);
    close_btn->setProperty("isWindowButton", 0x2);
    close_btn->setProperty("useIconHighlightEffect", 0x8);
    close_btn->setFlat(true);
    close_btn->setIcon(QIcon::fromTheme("window-close-symbolic"));

//    close_btn->set_btn_style(true);
    close_btn->setFixedSize(30,30);
    close_btn->move(this->width()-34,4);

    cancel_btn = new select_button();
    cancel_btn->setFixedSize(80,32);
    cancel_btn->set_btn_name("取消");
    cancel_btn->setStyleSheet("QPushButton{color:#444444;background-color:#d9d9d9;border-radius:4px;font-size:14px;}"
                                "QPushButton:hover{color:#ffffff;background-color:#6b8eeb;}"
                                "QPushButton:pressed{color:#ffffff;background-color:#415fc4;}");

    restart_btn = new select_button();
    restart_btn->setFixedSize(80,32);
    restart_btn->set_btn_name("立即重启");
    restart_btn->setStyleSheet("QPushButton{color:#444444;background-color:#d9d9d9;border-radius:4px;font-size:14px;}"
                               "QPushButton:hover{color:#ffffff;background-color:#6b8eeb;}"
                               "QPushButton:pressed{color:#ffffff;background-color:#415fc4;}");

    reduction_btn = new select_button();
    reduction_btn->setFixedSize(80,32);
    reduction_btn->set_btn_name("立即还原");
    reduction_btn->setStyleSheet("QPushButton{color:#444444;background-color:#d9d9d9;border-radius:4px;font-size:14px;}"
                               "QPushButton:hover{color:#ffffff;background-color:#6b8eeb;}"
                               "QPushButton:pressed{color:#ffffff;background-color:#415fc4;}");  

    determine_btn = new select_button();
    determine_btn->setFixedSize(80,32);
    determine_btn->set_btn_name("确定");
    determine_btn->setStyleSheet("QPushButton{color:#444444;background-color:#d9d9d9;border-radius:4px;font-size:14px;}"
                               "QPushButton:hover{color:#ffffff;background-color:#6b8eeb;}"
                               "QPushButton:pressed{color:#ffffff;background-color:#415fc4;}");

    determine_btn->move(160,57);
    determine_btn->hide();
    QHBoxLayout *hlayout1 = new QHBoxLayout();
    if(is_determine == false)
    {
        hlayout1->addStretch();
        hlayout1->addWidget(cancel_btn);
        hlayout1->addWidget(determine_btn);
        hlayout1->addSpacing(15);
        hlayout1->addWidget(restart_btn);
        hlayout1->addWidget(reduction_btn);
        hlayout1->addStretch();
    }
    else
    {
        hlayout1->addStretch();
        hlayout1->addWidget(determine_btn);
        hlayout1->addStretch();
    }

    QHBoxLayout *hlayout3 = new QHBoxLayout();
    hlayout3->addStretch();
    hlayout3->addWidget(tip_label);
    hlayout3->addStretch();

    QHBoxLayout *hlayout4 = new QHBoxLayout();
    hlayout4->addStretch();
    hlayout4->addWidget(massge_label);
    hlayout4->addStretch();

    QHBoxLayout *hlayout5 = new QHBoxLayout();
    hlayout5->addStretch();
    hlayout5->addWidget(close_btn);
    hlayout5->addSpacing(4);

    QVBoxLayout *vlayout1 = new QVBoxLayout();
    vlayout1->addLayout(hlayout5);
    vlayout1->addSpacing(45);
    vlayout1->addLayout(hlayout3);
    vlayout1->addSpacing(15);
    vlayout1->addLayout(hlayout4);
    vlayout1->addStretch();
    vlayout1->addLayout(hlayout1);
    vlayout1->addSpacing(25);
    vlayout1->setContentsMargins(0,4,0,0);

    setLayout(vlayout1);

}

Quit_Widget::~Quit_Widget()
{
    delete this;
}

//实现鼠标移动窗口
void Quit_Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        //移动中的鼠标位置相对于初始位置的相对位置.
        QPoint relativePos = event->globalPos() - this->m_startPoint;
        //然后移动窗体即可.
        this->move(this->m_windowPoint + relativePos );
    }

}

void Quit_Widget::mousePressEvent(QMouseEvent *event)
{
    //当鼠标左键点击时.
    if (event->button() == Qt::LeftButton)
    {
        this->m_move = true;
        this->m_startPoint = event->globalPos();
        this->m_windowPoint = this->frameGeometry().topLeft();
    }
}

void Quit_Widget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
       {
           //改变移动状态.
           this->m_move = false;
       }
}
